<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>美兰数据文化馆与图书馆</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="<%=request.getContextPath()%>/huazhi/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="<%=request.getContextPath()%>/huazhi/css/public.css" media="all">
    <link rel="shortcut icon" href="<%=request.getContextPath()%>/huazhi/images/favicon.ico"/>
</head>
<style>
    /* 底部固定区域 */
    .layui-layout-admin .layui-footer {
        padding: 10px 0;
        text-align: center;
    }

    .layui-layout-admin .layui-footer {
        background-color: #fff;
    }
</style>
<body>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <form class="layui-form" lay-filter="authtree-submit-form">
                <div class="layui-tab layui-tab-brief">
                    <ul class="layui-tab-title">
                        <li class="layui-this">基础信息</li>
                        <li>权限信息</li>
                    </ul>
                    <div class="layui-tab-content">
                        <div class="layui-tab-item layui-show">
                            <div class="layui-form-item">
                                <div class="layui-col-lg6">
                                    <div class="layui-col-xs11">
                                        <label class="layui-form-label">角色名称</label>
                                        <div class="layui-input-block">
                                            <input type="text" name="rolename" lay-verify="rolename"
                                                   autocomplete="off" placeholder="请输入角色名称"
                                                   class="layui-input" id="rolename">
                                        </div>
                                    </div>
                                    <div class="layui-col-xs1">
                                        <div class="layui-form-mid layui-word-aux"
                                             style="color: red !important;padding: 10px !important;">*
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <div class="layui-form-item">
                                <div class="layui-col-lg6">
                                    <div class="layui-col-xs11">
                                        <label class="layui-form-label">角色代码</label>
                                        <div class="layui-input-block">
                                            <input type="text" name="rolecode" lay-verify="rolecode"
                                                   autocomplete="off" placeholder="请输入角色代码"
                                                   class="layui-input">
                                        </div>
                                    </div>
                                </div>
                            </div>


                            <div class="layui-form-item">
                                <div class="layui-col-lg6">
                                    <div class="layui-col-xs11">
                                        <label class="layui-form-label">角色描述</label>
                                        <div class="layui-input-block">
                                                <textarea type="text" name="remark"
                                                      autocomplete="off" placeholder="请输入角色描述"
                                                      class="layui-textarea"></textarea>
                                        </div>
                                    </div>
                                </div>
                            </div>


                        </div>
                        <div class="layui-tab-item">
                            <div class="layui-row">
                                <div class="layui-col-md3">
                                    <div class="layui-form-item">
                                        <label class="layui-form-label"></label>
                                        <div class="layui-input-block">
                                            <div id="LAY-auth-tree-index0"></div>
                                        </div>
                                    </div>
                                </div>
                                <div class="layui-col-md3">
                                    <div class="layui-form-item">
                                        <label class="layui-form-label"></label>
                                        <div class="layui-input-block">
                                            <div id="LAY-auth-tree-index1"></div>
                                        </div>
                                    </div>
                                </div>
                                <div class="layui-col-md3">
                                    <div class="layui-form-item">
                                        <label class="layui-form-label"></label>
                                        <div class="layui-input-block">
                                            <div id="LAY-auth-tree-index2"></div>
                                        </div>
                                    </div>
                                </div>
                                <div class="layui-col-md3">
                                    <div class="layui-form-item">
                                        <label class="layui-form-label"></label>
                                        <div class="layui-input-block">
                                            <div id="LAY-auth-tree-index3"></div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item layui-layout-admin">
                    <div class="layui-footer" style="left: 0;padding: 0px;">
                        <button class="layui-btn" lay-submit lay-filter="*" id="pushButton">立即提交</button>
                    </div>
                </div>
            </form>

        </div>
    </div>
</div>
<script src="<%=request.getContextPath()%>/huazhi/lib/layui-v2.5.5/layui.all.js"></script>
<script src="<%=request.getContextPath()%>/huazhi/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script>
    layui.use(['form', 'laydate', 'authtree'], function () {
        var $ = layui.$
            , form = layui.form
            , laydate = layui.laydate
            , layer = layui.layer
            , authtree = layui.authtree;

        $.ajax({
            type: "post",
            url: '<%=request.getContextPath()%>/rest/system/getMenuRoleList',
            dataType: "json",
            async: false,
            success: function (res) {
                if (res != null) {
                    for (var i = 0; i < res.length; i++) {
                        authtree.render('#LAY-auth-tree-index' + i, res[i].data.trees, {
                            inputname: 'permission' + i
                            // , layfilter: 'lay-check-auth'
                            , openall: true
                            , theme: 'auth-skin-default'
                            , themePath: '<%=request.getContextPath()%>/huazhi/js/lay-module/authtree/'
                            , autowidth: true
                            , formFilter: 'authtree-submit-form' // 注意！！！如果不与其他插件render冲突，这个选填
                        });
                    }
                }

            }
        })


        //自定义验证规则
        form.verify({
            rolename: function (value, item) {
                if (value == "") {
                    return '请输入角色名称';
                }
            },
        });


        form.on('submit(*)', function (data) {
            var field = data.field//当前容器的全部表单字段，名值对形式：{name: value}
            var rolename = field.rolename;
            // if(rolename==""){
            //     var rolenames= document.getElementById("rolename");
            //     rolenames.setAttribute("class", "layui-input layui-form-danger");
            //     rolenames.focus();
            //     layer.msg('角色名称必填', {icon: 5,shift: 0});
            //     return false;
            // }
            var flag = true;
            $.ajax({
                type: "post",
                url: "<%=request.getContextPath()%>/rest/system/existsRole",
                data: "rolename=" + rolename,
                dataType: "json",
                async: false,
                success: function (data) {
                    if (data == false) {
                        flag = false;
                    }
                }
            })

            if (flag) {
                var rolenames = document.getElementById("rolename");
                rolenames.setAttribute("class", "layui-input layui-form-danger");
                rolenames.focus();
                layer.msg('角色名称已存在,请重新输入', {icon: 5, anim: 6});
                return false;
            }
            var authids0 = authtree.getChecked('#LAY-auth-tree-index0');
            data.field.permission0 = authids0;
            var authids1 = authtree.getChecked('#LAY-auth-tree-index1');
            data.field.permission1 = authids1;
            var authids2 = authtree.getChecked('#LAY-auth-tree-index2');
            data.field.permission2 = authids2;
            var authids3 = authtree.getChecked('#LAY-auth-tree-index3');
            data.field.permission3 = authids3;

            var index = layer.msg('数据提交中，请稍候', {icon: 16, time: false, shade: 0.6});
            $("#pushButton").addClass("layui-btn layui-btn-disabled").prop("disabled", true);

            $.ajax({
                type: "post",
                url: "<%=request.getContextPath()%>/rest/system/addRole",
                data: field,
                dataType: "json",
                async: false,
                success: function (res) {
                    setTimeout(function () {
                        layer.close(index);
                        if (res == true) {
                            layer.msg('添加成功');
                        } else {
                            layer.msg(res.errorMsg);
                        }
                    }, 2000);
                    setTimeout(function () {
                        var indexs = parent.layer.getFrameIndex(window.name);//获取当前弹出层的层级
                        parent.layer.close(indexs);//关闭弹出层
                    }, 4000);
                }
            })
            return false;
        });


    });
</script>
</body>
</html>


